package com.bawei.module_mine.viewmodel

import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_mine.repository.RegisterRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class RegisterViewModel @Inject constructor(private val repository: RegisterRepository):BaseViewModel(){
    private val _mNotice= MutableStateFlow<RequestEvent>(Loading)
    val noticeState
        get() =_mNotice.asStateFlow()
    fun register(username:String,password:String){
        execute(
            action = repository.register(username,password),
            onStart = {
                _mNotice.value=Loading
            },
            onError = {
                _mNotice.value= Failed(it)
            },
            onFinish = {
                _mNotice.value= Success
                AppMMKV.username=it.data.username
                AppMMKV.password=it.data.password
            }
        )
    }
}